package main.java.房屋租借系统.tool;

import main.java.房屋租借系统.JavaBeen.House;
import main.java.房屋租借系统.JavaBeen.User;
import main.java.房屋租借系统.dao.HouseSerive;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;
import java.util.Random;
import java.util.Scanner;

/**
 * @Auther:Liu
 * @Testname:HouseOppen
 * @Date:2025/3/20 10:09
 */
public class HouseOppen {
    boolean falge = true;
    private QueryRunner queryRunner = new QueryRunner();

    //1.查询所有数据
    public List<House> selectAll() throws Exception {
        List<House> query = queryRunner.query(HouseSerive.getConnection(), "select * from homemange;", new BeanListHandler<House>(House.class));
        return query;
    }

    //2.删除一条数据
    public int delete(int Id) throws Exception {
        int i = queryRunner.update(HouseSerive.getConnection(), "delete from homemange where Id=?;", Id);
        return i;
    }

    //3.更新一条数据
    public int update(House house) throws Exception {
        int i = queryRunner.update(HouseSerive.getConnection(), "update homemange set Name=?,Phone=?,Address=?,Rent=?,State=? where Id=?;", house.getName(), house.getPhone(), house.getAddress(), house.getRent(), house.getState(), house.getId());
        return i;
    }

    //4.查询一条数据
    public House seleteone(int Id) throws Exception {
        House query = (House) queryRunner.query(HouseSerive.getConnection(), "select * from homemange where Id=?;", new BeanHandler<House>(House.class), Id);
        return query;
    }

    //5.插入一条数据
    public int insert(House house) throws Exception {
        int i = queryRunner.update(HouseSerive.getConnection(), "insert into homemange(Name,Phone,Address,Rent,State) values(?,?,?,?,?);", house.getName(), house.getPhone(), house.getAddress(), house.getRent(), house.getState());
        return i;
    }

    //6.退出
    public void broke() {
        falge = false;
        System.out.println("程序已退出!");
    }

    //7.登录
    public User load(String username, String password) throws Exception {
        User query = queryRunner.query(HouseSerive.getConnection(), "select * from user where username='" + username + "'and password='" + password + "';", new BeanHandler<User>(User.class));
        return query;
    }

    /**
     * 功能： 读取⼀个字符串
     *
     * @param limit       读取的⻓度
     * @param blankReturn 如果为true ,表示 可以读空字符串。
     *                    如果为false表示 不能读空字符串。
     *                    <p>
     *                    如果输⼊为空，或者输⼊⼤于limit的⻓度，就会提示重新输⼊。
     * @return
     */
    public static String readKeyBoard(int limit, boolean blankReturn) {
        Scanner scanner = new Scanner(System.in);
//定义⼀个字符串
        String line = "";
        while (scanner.hasNextLine()) {//判断是否有下⼀⾏
            line = scanner.nextLine();
//如果line.length=0, 即⽤户没有输⼊任何内容，直接回⻋
            if (line.length() == 0) {
                if (blankReturn) return line;//如果blankReturn=true,可以返回空串
                else continue; //如果blankReturn=false,不接受空串，必须输⼊内容
            }
//如果⽤户输⼊的内容⼤于了 limit，就提示重写输⼊
//如果⽤户如的内容 >0 <= limit ,我就接受
            if (line.length() < 1 || line.length() > limit) {
                System.out.print("输⼊⻓度（不能⼤于" + limit + "）错误，请重新输⼊：");
                continue;
            }
            break;
        }
        return line;
    }


    /**
     * 功能：读取键盘输⼊的指定⻓度的字符串
     *
     * @param limit 限制的⻓度
     * @return 指定⻓度的字符串
     */
    public static String readString(int limit) {
        return readKeyBoard(limit, false);
    }


    /**
     * 功能：读取键盘输入的指定长度的字符串或默认值，如果直接回车，返回默认值，否则返回字符串
     * @param limit 限制的长度
     * @param defaultValue 指定的默认值
     * @return 指定长度的字符串
     */

    public static String readString(int limit, String defaultValue) {
        String str = readKeyBoard(limit, true);
        return str.equals("")? defaultValue : str;
    }

    /**
     * 功能：读取键盘输⼊的 整数或默认值，如果直接回⻋，则返回默认值，否则返回输⼊的整数
     *
     * @param defaultValue 指定的默认值
     * @return 整数或默认值
     */
    public static int readInt(int defaultValue) {
        int n;
        for (; ; ) {
            String str = readKeyBoard(10, true);
            if (str.equals("")) {
                return defaultValue;
            }
//异常处理
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.println("数字输⼊的有错误，请重新输⼊！");
            }
        }
        return n;
    }


    //验证码方法
    public static String Ma(){
        Random random = new Random();
        String str = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ";
        String str1="";
        for (int i = 0; i <5 ; i++) {
            int i1 = random.nextInt(str.length());
            str1+=str.charAt(i1);
        }
       return str1;
    }

}




